import 'package:base_moudle/index.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';

import '../../../config/zs_routers.dart';
import '../../../models/data_model/HyWorkModel.dart';
import '../../../new_paltform/define/hy_header.dart';
import 'work_setting_logic.dart';

class HyWorkSettingPage
    extends HYGetXStatelessBasePage<HyWorkSettingLogic, Object> {
  static Future? push(List<ItemModel> arr) {
    return Get.toNamed(ZsRouter.zsWorkSettingRouter, arguments: arr);
  }

  @override
  String get naviTitle => '工作台设置';

  @override
  List<Widget>? actions() {
    return [
      TextButton(
          onPressed: () {
            Get.back(result: List<ItemModel>.from(controller.items.value));
          },
          child: ZsText(
            '完成',
            fontWeight: FontWeight.w500,
            fontSize: 16,
            color: zs_mainColor,
          ))
    ];
  }

  @override
  Widget? buildTopView(Object? object) {
    return ZsText(
      '个人常用应用',
      fontSize: 16,
      fontWeight: FontWeight.bold,
    ).intoContainer(
        alignment: Alignment.centerLeft,
        padding: const EdgeInsets.fromLTRB(16, 20, 16, 16));
  }

  @override
  Widget buildWidget(BuildContext? context, Object? object) {
    return Obx(() => HYContainer(
          margin: const EdgeInsets.all(16),
          backgroundColor: Colors.transparent,
          clipBehavior: Clip.hardEdge,
          borderRadius: BorderRadius.circular(12),
          child: ReorderableListView.builder(
              itemBuilder: (BuildContext context, int index) {
                final ItemModel item = controller.items[index];
                return SwipeActionCell(
                    key: ValueKey(item),
                    trailingActions: [
                      SwipeAction(
                          title: '移除',
                          onTap: (CompletionHandler handler) async {
                            if (controller.items.length >= 2) {
                              await handler(true);
                              controller.items.removeAt(index);
                            } else {
                              EasyLoading.showToast('无法删除,最少保留一个');
                            }
                          })
                    ],
                    backgroundColor: Colors.white,
                    child: _buildCell(index, item));
              },
              itemCount: controller.items.length,
              onReorderStart: (int c) {
                HapticFeedback.mediumImpact();
              },
              onReorderEnd: (int c) {
                HapticFeedback.mediumImpact();
              },
              onReorder: (int oldIndex, int newIndex) {
                if (oldIndex < newIndex) {
                  newIndex -= 1;
                }
                final ItemModel child = controller.items.removeAt(oldIndex);
                controller.items.insert(newIndex, child);
              }),
        ));
  }

  Widget _buildCell(int index, ItemModel model) {
    return HYContainer(
      padding: const EdgeInsets.all(16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              HYContainer(
                alignment: Alignment.center,
                backgroundColor: ColorHex('#FF4D64'),
                height: 20,
                width: 20,
                borderRadius: BorderRadius.circular(10),
                child: const Icon(
                  Icons.remove,
                  color: Colors.white,
                  size: 14,
                ),
                onTap: () {
                  if (controller.items.length >= 2) {
                    controller.items.removeAt(index);
                  } else {
                    EasyLoading.showToast('无法删除,最少保留一个');
                  }
                },
              ),
              if (model.iconName.contains('http'))
                CachedNetworkImage(
                  imageUrl: model.iconName,
                  width: 40,
                  height: 40,
                  fit: BoxFit.cover,
                  errorWidget: (BuildContext c, String s, i) {
                    return Container(
                      // margin: EdgeInsets.all(15),
                      width: 40, height: 40,
                      decoration: BoxDecoration(
                          color: zs_color_f2,
                          borderRadius: BorderRadius.circular(8)),
                    );
                  },
                ).intoContainer(
                    color: Colors.white,
                    margin: const EdgeInsets.only(left: 10, right: 10))
              else
                Image.asset(
                  model.iconName,
                  width: 40,
                  height: 40,
                ).marginOnly(left: 12, right: 12),
              ZsText(model.title),
            ],
          ),
          Image.asset(
            'assets/images/project/work_store.png',
            width: 18,
            height: 14,
          ),
        ],
      ),
    );
  }
}
